﻿document.write("<script language='javascript' src='/resources/js/views/defineTable.js'></script>");
document.write("<script language='javascript' src='/resources/js/views/defineInit.js'></script>");
document.write("<script language='javascript' src='/resources/js/views/commonFunction.js'></script>");
var departIndictor="/report/analysis/depart.do";
var departIndictorMove="/department/indicator/move.do";
var action = "/indicator/sameType.do";//加载指标
var allField="/indicator/type/list.do"
var page = 1;
var pageSize = 500;

function loadGRID(){
	var year=$('#syearTemp').val();
	var iHeight = document.documentElement.clientHeight;
	$('#tt').datagrid( {
		width : '100%',
		height : iHeight-60,
		toolbar: defineInitCreateToolBar(true,false,true,true),
		fitColumns : true,
		columns : getDefineTableReportColumnsDepartFormsValue(year),
		sortable:true,
		sortName:"code",//排序
		multiSort:true, //定义是否允许多列排序
		remoteSort:false,//表示前端排序不请求服务器
		sortOrder:'asc',
		rownumbers: false,
		singleSelect:false // 是否单选
   });
}

function loadIndicatorTypeField(){

	var formsid=window.parent.str.split(',')[3];

	$.getJSON(allField,{'formsid':formsid}, function (json) {
		var data = json
		if(data.length<1){
			return
		}
		$('#ttIndicatorType').datagrid( {
			columns : [
				defineTableIndicatorTypeColumnsName
			],
			data : data,
			onLoadSuccess:function () {
				loadIndicatorField();
			},
			onSelect:function () {
				loadIndicatorField();
			},
			onUnselect:function () {
				loadIndicatorField();
			},
			onSelectAll:function () {
				loadIndicatorField();
			}

		});
	},'json');
}

function loadIndicatorField(){

	var indicatorType="";
	var row=$('#ttIndicatorType').datagrid('getSelections');
	for (var i = 0; i < row.length; i ++){
		indicatorType+=row[i]['id']+",";
	}
	indicatorType = indicatorType.substring(0, indicatorType.length - 1);
	if(indicatorType==""){
		indicatorType=11;
	}

	var formsid=window.parent.str.split(',')[3];
	$.get(action, {"type":indicatorType,"formsid":formsid}, function (json) {
		if(json.code!=200){
			$('#ttindicator').datagrid( {data:[]});
		}else{
			var data=json.data;
			$('#ttindicator').datagrid( {
				data:data.results,
				columns: [
					defineTableIndicatorColumnsNames
				]
			});
		}
	},'json');
}

//根据年份、部门id获取部门指标方法
function query(curPage, pageSize){
    $("#departidF").val(window.parent.str.split(',')[0]);
    $("#syearTemp").val(window.parent.str.split(',')[1]);
    $("#formid").val(window.parent.str.split(',')[3]);
	
	var years=$('#syearTemp').val();
	var departid=$('#departidF').val();
	var formsid=$('#formid').val();
	var param={"years" : years,"departid" : departid,"formsid" : formsid,"page" : curPage,"pageSize" : pageSize}

	$.get(departIndictor,param,function(json){
		 var code=json.code;
		 if(code==200){
			 var data=json.data.results;
			 var total = json.data.size;
			 var totalPage = Math.ceil(total / pageSize);
			 var start = (curPage-1) * pageSize;
			 $('#tt').datagrid( {
				 data:data,
				 pagination: true,//打开翻页功能
				 pageNumber: curPage,//显示数据列用
                 pageSize: pageSize,
                 pageList: [500]
			 });
			 var p = $('#tt').datagrid('getPager');
			 $(p).pagination({
	                total: total,
	                pageList: [500],
	                pageSize: pageSize,
	                pageNumber: curPage,
	                beforePageText: '第',
	                afterPageText: '页    共 ' + totalPage + ' 页',
	                displayMsg: '当前显示 ' + (start + 1) + '-' + (start + data.length) + ' 条记录 共 ' + total + ' 条记录',
	                onSelectPage: function (curPage, pageSize) {
	                	query(curPage, pageSize);
	                },
	                onChangePageSize: function (ps) {
	                    pageSize = ps;
	                }
			 });
		 }else{
			 $('#tt').datagrid( {data:[]});
		 }
	},'json');
}

window.onload=function(){
	loadGRID();
}

$(document).ready(function () {
	// loadGRID();
	// loadField();
	$('#fmAdd').form( {
		success : function(data) {
		    CRUD.queryList();
		    try{
			    var result = eval('(' + data + ')');
			    if(result.success == true){
			    	$('#dialogAdd').dialog('close');
					$('#dialogMove').dialog('close');
				 } else {
			        $.messager.alert('提示', '保存失败');
				 }
			}catch(err){
				
			}
		}
	});
	
    // 确定添加
    $('#a_AddOK').click(function () {
    	$('#departid').val(window.parent.str.split(',')[0]);
    	$('#year').val(window.parent.str.split(',')[1]);
    	$('#formsDepart').val(window.parent.str.split(',')[2]);
    	$('#formidA').val(window.parent.str.split(',')[3]);

		var indicatorids="";
		var row = $('#ttindicator').datagrid('getSelections');
		for (var i = 0; i < row.length; i ++){
			indicatorids+=row[i]['id']+",";
		}
		indicatorids = indicatorids.substring(0, indicatorids.length - 1);
		if(indicatorids==null){
			$.messager.alert('提示', '请选择指标');
			return;
		}

		$('#indicatorid').val(indicatorids);

        $('#fmAdd').submit();
    });

    // 取消
    $('#a_AddCancle').click(function () {
    	var textarea = $('#idScales');
    	if (textarea) {
    		textarea.remove();
    	}
        $('#fmAdd').form('clear');
        $('#dialogAdd').dialog('close');
        $('#tt').datagrid("clearSelections");
    });

	// 确定转移
	$('#a_MoveOK').click(function () {
		if (!$('#toDepartId').combobox("getValue")) {
			$.messager.alert('提示', '请选择复制目标部门!');
			return;
		}
		if (!$('#toYear').combobox("getValue")) {
			$.messager.alert('提示', '请选择对应年份!');
			return;
		}
		if (!$('#indicatorIds').text("getValue")) {
			$.messager.alert('提示', '请选择对应指标!');
			return;
		}
		// $('#fmMove').submit();
		fmMoveSubmit();

		$('#dialogMove').dialog('close');
		location.reload();
		CRUD.queryList();
	});

//加载领域信息下拉列表
function fmMoveSubmit(){

	var formsid=window.parent.str.split(',')[3];
	var toDepartId= $('#toDepartId').combobox("getValue");
	var indicatorIds = $('#indicatorIds').val();
	var fromDepartId = $("#fromDepartId").val();
	var fromYear = $("#fromYear").val();
	var toYear= $('#toYear').combobox("getValue");

	$.getJSON(departIndictorMove,{'formsid':formsid,'indicatorIds':indicatorIds,'fromDepartId':fromDepartId,'toDepartId':toDepartId,'fromYear':fromYear,'toYear':toYear}, function (json) {
		var code = json.code;
		if(code==200){
			$.messager.alert('成功', '提交成功','success');
			return;
		}else{
			$.messager.alert('失败', '转移失败');
		}
		return;
	},
	'json');
}

	// 转移取消
	$('#a_MoveCancle').click(function () {
		var textarea = $('#idScales');
		if (textarea) {
			textarea.remove();
		}
		$('#fmMove').form('clear');
		$('#dialogMove').dialog('close');
		$('#tt').datagrid("clearSelections");
	});
    //departCommon.setDepart();				//加载省级部门下拉列表
  //  commonIndicator.setIndicator();			//加载地市级部门下拉列表
    InitForm();
    CRUD.queryList();
});

var CRUD = {
    queryList: function () {
    	query(page, pageSize)
    },

    Add: function () {
		loadIndicatorTypeField();
    	$('#fmAdd').form('clear');
    	var textarea = $('#idScales');
    	if (textarea) {
    		textarea.remove();
    	}
        $('#dialogAdd').dialog('open');
        $("#sourceid").combobox('select',window.parent.str.split(',')[0]);
        $('#tt').datagrid("clearSelections");
        $('#dialogAdd').dialog("setTitle", "添加指标分类");
    },
    
    Del: function () {
        var r = $('#tt').datagrid('getSelected');
        if (r) {
            $.messager.confirm('删除提示', '你确认删除这一条数据吗？', function (d) {
                if (d) {
                    $.get(getContextPath() + "/report/delete.do?id=" + r.id, function (data) {
                        if (data == 'true') {
                            $.messager.show(
                                {
                                    title: '系统提示',
                                    msg: '删除成功！',
                                    timeout: 1500
                                });
                            $('#tt').datagrid("clearSelections");
                            CRUD.queryList();
                        } else {
                            $.messager.alert('错误', '删除失败！');
                        }
                    });
                }
            });
        } else {
            $.messager.alert('提示', '请选择一行数据!');
        }
    },
    // Update: function () {
	// 	// loadIndicatorTypeField();
    //     var r = $('#tt').datagrid('getSelected');
    //     if (r == null) {
    //         $.messager.alert('提示', '请选择一行数据');
    //         return;
    //     }
    //     $('#dialogAdd').dialog('open');
    //     $("#id").val(r.id);
    //     $("#indicatorType").combobox('setValue',r.indicatorType);
    //     $("#sourceid").combobox('setValue',r.departid);
    //     // $("#indicatorid").combobox('setValue',r.indicator);
    //     $("#remark").textbox('setValue',r.remark);
    //     $('#dialogAdd').dialog("setTitle", "修改指标数据");
    // },

	Move: function () {

		var row = $('#tt').datagrid('getSelections');
		// var row = $('#tt').datagrid('getSelected');
		if (row == null) {
			$.messager.alert('提示', '请选择一行数据');
			return;
		}
		$('#dialogMove').dialog('open');
		// isCheck.push('check');
		var indicators=saveMove(row);
		$('#indicatorIds').val(indicators);
		$("#fromDepartId").val(row[0].departid);
		var year = window.parent.str.split(',')[1];
		$('#fromYear').val(year);
		$("#toYear").combobox('setValue',year);
		$("#toDepartId").combobox('setValue',row[0].departid);
		$('#dialogMove').dialog("setTitle", "转移指标数据");
	}
};

function saveMove(row) {
	if (row == null) {
		return;
	}
	var indicators="";
	for (var i = 0; i < row.length; i ++){
		indicators += row[i]['indicator']+",";
	}
	return indicators.substring(0, indicators.lastIndexOf(','));
}

function modify(id,sformsDepart,depart,sourceid,sforms,indicator,code,syear,unit,value,aim,need,hide,remark) {
	remark = setEmptyForm(remark);
	aim = setEmptyForm(aim);
    $('#dialogAdd').dialog('open');
    $('#fmAdd').form('load',{
    		id: id,
    		sformsDepart: sformsDepart,
    		depart: depart,
    		sourceid: sourceid,
    		sforms: sforms,
    		indicator: indicator,
    		code: code,
    		syear: syear,
    		unit: unit,
    		value: value,
    		aim: aim,
    		need: need,
    		hide: hide,
    		remark: remark
    	});
    $('#dialogAdd').dialog("setTitle", "修改指标数据");
}
	
function del(id) {
	$.messager.confirm('删除该指标数据', '你确认删除该指标数据吗?', function (d) {
		if (d) {
			$.get(getContextPath() + "/report/delete.do?id=" + id, function (data) {
				if (data == 'true') {
					$.messager.show({
						title: '系统提示',
						msg: '删除成功！',
						timeout: 1500
					});
					$('#tt').datagrid("clearSelections");
					CRUD.queryList();
				} else {
					$.messager.alert('错误', '删除失败！');
				}
			});
		}
	});
}

function InitForm() {
    var width = '80%';
    // var height = '60%';
    var height = window.screen.height;
    if (height > 800) {
        // height = 800;
		height = 0.85*height;
    }
    $('#dialogAdd').dialog({
        closed: true,
        title: "添加指标数据",
        width: width,
        height: height
    });
	$('#dialogMove').dialog({
		closed: true,
		title: "转移指标数据",
		width: width,
		height: height
	});
}

function setEmptyForm(value) {
	if(typeof(value) == "undefined"||value == "null"){
		return "";
	}else{
		return value;
	}
}

window.onresize = function () {
	var iHeight = document.documentElement.clientHeight;
	$("#tt").datagrid('resize',{height:iHeight-60+'px'});
}
